package com.walixiwa.player.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dueeeke.videoplayer.controller.ControlWrapper;
import com.dueeeke.videoplayer.controller.IControlComponent;
import com.dueeeke.videoplayer.player.VideoView;
import com.walixiwa.videoviewcontral.R;

/**
 * 播放器顶部标题栏
 */
public class DetailVodScaleView extends FrameLayout implements IControlComponent {

    private ControlWrapper mControlWrapper;

    private TextView mTvAuto;
    private TextView mTvFill;
    private TextView mTvOriginal;
    private TextView mTvCrop;
    private TextView mTv16_9;
    private TextView mTv4_3;

    public DetailVodScaleView(@NonNull Context context) {
        super(context);
    }

    public DetailVodScaleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DetailVodScaleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        this.setVisibility(GONE);
        LayoutInflater.from(getContext()).inflate(R.layout.detail_layout_scale_view, this, true);
        mTvAuto = findViewById(R.id.tv_auto);
        mTvFill = findViewById(R.id.tv_fill);
        mTvOriginal = findViewById(R.id.tv_original);
        mTvCrop = findViewById(R.id.tv_crop);
        mTv16_9 = findViewById(R.id.tv_16_9);
        mTv4_3 = findViewById(R.id.tv_4_3);
        mTvAuto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(0);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_DEFAULT);
                    onScaleClickListener.onScaleClick("自动适应");
                }
            }
        });
        mTvFill.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(1);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
                    onScaleClickListener.onScaleClick("填充屏幕");
                }
            }
        });
        mTvOriginal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(2);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_ORIGINAL);
                    onScaleClickListener.onScaleClick("原始尺寸");
                }
            }
        });
        mTvCrop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(3);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);
                    onScaleClickListener.onScaleClick("居中裁剪");
                }
            }
        });
        mTv16_9.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(4);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_16_9);
                    onScaleClickListener.onScaleClick("16 : 9");
                }
            }
        });
        mTv4_3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onScaleClickListener != null) {
                    setChecked(5);
                    mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_4_3);
                    onScaleClickListener.onScaleClick("4 : 3");
                }
            }
        });
    }

    @Override
    public void attach(@NonNull ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animation anim) {
        if (isVisible) {
            setVisibility(GONE);
        }
    }

    @Override
    public void onPlayStateChanged(int playState) {
        setVisibility(GONE);
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        setVisibility(GONE);
    }

    @Override
    public void setProgress(int duration, int position) {

    }

    @Override
    public void onLockStateChanged(boolean isLocked) {

    }

    private OnScaleClickListener onScaleClickListener;

    public void setOnScaleClickListener(OnScaleClickListener onScaleClickListener) {
        this.onScaleClickListener = onScaleClickListener;
    }

    public interface OnScaleClickListener {
        void onScaleClick(String name);
    }

    private void setChecked(int index) {
        int checkedColor = getResources().getColor(R.color.colorPrimary);
        int defaultColor = Color.WHITE;
        mTvAuto.setTextColor(index == 0 ? checkedColor : defaultColor);
        mTvFill.setTextColor(index == 1 ? checkedColor : defaultColor);
        mTvOriginal.setTextColor(index == 2 ? checkedColor : defaultColor);
        mTvCrop.setTextColor(index == 3 ? checkedColor : defaultColor);
        mTv16_9.setTextColor(index == 4 ? checkedColor : defaultColor);
        mTv4_3.setTextColor(index == 5 ? checkedColor : defaultColor);
        setVisibility(GONE);
    }

    public void reset() {
        mControlWrapper.setScreenScaleType(VideoView.SCREEN_SCALE_DEFAULT);
        setChecked(0);
    }

    public void show() {
        mControlWrapper.hide();
        this.setVisibility(VISIBLE);
        bringToFront();
    }
}
